package moodish.comm;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;

/**
 * @author Grupo26
 * @version 0.1
 */

public class ClientCommClass implements ClientComm{
	
	private int PORTA;
	private ObjectInputStream dIn;
	private ObjectOutputStream dOut;
	private String nickName;
	private Socket clientSocket;
	private boolean isConnected;
	private LinkedList<ClientSideMessageClass> ClientSideMessageClassList;
	
	public ClientCommClass(int port){
		
		PORTA = port;
		
	}
       
	@Override
	public void connect(String host, String nickname)throws UnknownHostException, IOException {
		
		ClientSideMessageClassList = new LinkedList<ClientSideMessageClass>();
		
		clientSocket = new Socket(host, PORTA);
		dOut=new ObjectOutputStream(clientSocket.getOutputStream());
		
		nickName = nickname;
		ServerSideMessageClass ssm = new ServerSideMessageClass(null,nickName,ServerSideMessage.Type.CLIENT_CONNECTED);
		dOut.writeObject(ssm);
		dOut.flush();
		
		isConnected = true;
		
		try {
			Client clientThread =  new Client();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public boolean isConnected() {
		
		boolean ret = isConnected;
		return ret;
		
	}

	@Override
	public void disconnect() {
		
			try {
				
				ServerSideMessageClass ssm = new ServerSideMessageClass(null,nickName,ServerSideMessage.Type.CLIENT_DISCONNECTED);
				dOut.writeObject(ssm);
				dOut.flush();
				dOut.close();
				isConnected = false;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	@Override
	public ClientSideMessage getNextMessage() {
		
		while(ClientSideMessageClassList.size()<=0){
			
		}
		
		if(isConnected){
			try
			{
				
				ClientSideMessageClass csm = ClientSideMessageClassList.getFirst();
				ClientSideMessageClassList.removeFirst();
				return csm;
				
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		return null;
	}

	@Override
	public boolean hasNextMessage() {
		
		boolean ret = false;
		
		if(ClientSideMessageClassList.size()>0){
			ret = true;
		}
		
		return ret;
		
	}

	@Override
	public void sendMoodishMessage(String moodishMessage) {
		
		try {
			
			
			ServerSideMessageClass ssm = new ServerSideMessageClass(moodishMessage,nickName,ServerSideMessage.Type.CLIENT_CONNECTED);
			dOut.writeObject(ssm);
			dOut.flush();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void friendship(String nickname) {
		
		
	}

	@Override
	public void unfriendship(String nickname) {
		
		
	}

	
	public class Client extends Thread {
		
		
		public Client() throws Exception {		
			
			start();
			
		}
	    
		public void run() {
			
			while(isConnected)
			{
				try
				{
					dIn=new ObjectInputStream(clientSocket.getInputStream());
					ClientSideMessageClass csm = (ClientSideMessageClass)dIn.readObject();  
					//System.out.println( "\n" + csm.getPayload());
					ClientSideMessageClassList.add(csm);
				}
				catch(Exception ex)
				{
						ex.printStackTrace();
				}
			}
	        
	    }
	}
		
		
	
}
